# -*- coding: iso-8859-15 -*-
# 
# LICENSE:
# 
# Copyright 2008 Håkan Lindqvist. All rights reserved.
# 
# Redistribution and use in source and binary forms, 
# with or without modification, are permitted provided 
# that the following conditions are met:
# 
#    1. Redistributions of source code must retain the 
#       above copyright notice, this list of conditions 
#       and the following disclaimer.
#    2. Redistributions in binary form must reproduce the 
#       above copyright notice, this list of conditions 
#       and the following disclaimer in the documentation 
#       and/or other materials provided with the distribution.
# 
# THIS SOFTWARE IS PROVIDED BY HÅKAN LINDQVIST ``AS IS'' 
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 
# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL HÅKAN LINDQVIST 
# OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 
# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
# THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 
# DAMAGE.
# 

from adt.http          import HttpServer
from adt.report.Report import Report
from adt.report.Report import ReportIndex
from adt.report.Report import ReportTitle
from adt.report.Report import Heading
from adt.report.Report import Paragraph
from adt.report.Report import PageBreak


class TradeReportsRequestHandler(HttpServer.RequestHandler):

	def getPDFTestReport():
		# Read in the report's data
		report   = Report(out="test")
		title    = ReportTitle(title='Foo test report', abstract='A simple test report with an abstract that is only there to occupy some text.')
		index    = ReportIndex()

		report.add(title)
		report.add(index)

		report.add(Heading(text="First1", level=1))
		report.add(Paragraph("bla bla bla"))
		report.add(PageBreak())
		report.render("application/pdf")

		try:
			f   = file("test.pdf", mode='rb')
			pdf = f.read()

			f.close()
		except IOError, message:
			raise Exception(str(message))

		return pdf
	
	def getHTMLTestReport():
		# Read in the report's data
		report   = Report(out="test")

		title    = ReportTitle(title='Foo test report', abstract='A simple test report with an abstract that is only there to occupy some text.')
		index    = ReportIndex()

		report.add(title)
		report.add(index)

		report.add(Heading(text="First1", level=1))
		report.add(Paragraph("bla bla bla"))
		report.add(PageBreak())

		report.render("text/html")

		try:
			f    = file("test.html", mode='r')
			html = f.read()

			f.close()
		except IOError, message:
			raise Exception(str(message))

		return html

	# Define names and how to generate the appropriate reports
	reports            = { 'test.pdf' : [getPDFTestReport, 'application/pdf', 'A test report providing a simple sample of a report'] , 'test.html' : [getHTMLTestReport, 'text/html', 'Small test report to add another entry'] }

	# The virtual directory from which the reports defined
	# above should be fetched from
	virtual_report_dir = 'trade_reports_dir'

	# Virtual dirrectory which will list the available reports
	virtual_list_dir   = 'trade_reports'

# Start the HTTP server
if __name__ == '__main__':
	HttpServer.start_server(handler_class=TradeReportsRequestHandler)

